package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.model.domain.User;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.model.dto.vo.Id_BuildingVO;
import com.example.mybatisplus.model.dto.vo.Stats4BuildingVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.BuildingsService;
import com.example.mybatisplus.model.domain.Buildings;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2024-07-16
 * @version v1.0
 */
@RestController
@RequestMapping("/api/buildings")
public class BuildingsController {

    private final Logger logger = LoggerFactory.getLogger( BuildingsController.class );

    @Autowired
    private BuildingsService buildingsService;

    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        Buildings  buildings =  buildingsService.getById(id);
        return JsonResponse.success(buildings);
    }


    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        buildingsService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateBuildings(Buildings  buildings) throws Exception {
        buildingsService.updateById(buildings);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建Buildings
    *
    */
    @GetMapping("insert")
    public JsonResponse create(Buildings  buildings) throws Exception {
        buildingsService.save(buildings);
        return JsonResponse.success(null);
    }
    @GetMapping("getBuildingById")
    public JsonResponse getBuildingById(){
        List<Stats4BuildingVO> list = buildingsService.getBuildingById();
        return JsonResponse.success(list);
    }
    @GetMapping("buildingList")
    public JsonResponse pageList(Buildings buildings, PageDTO dto){
        Page<Buildings> page = buildingsService.pageList(buildings,dto);
        return JsonResponse.success(page);
    }
    @GetMapping("buildingListPlus")
    public JsonResponse buildingListById(Integer id){
        List<Id_BuildingVO>  list= buildingsService.buildingListById(id);
        return JsonResponse.success(list);
    }

    @PostMapping("editBuilding")
    public JsonResponse editBuilding(@RequestBody Buildings buildings){
        boolean b = buildingsService.updateById(buildings);
        return JsonResponse.success(b);
    }

}

